package com.m.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ApplicationUtil {
    private static ApplicationContext applicationContext;

    /** 初始化 */
    public static void setApplicationContext(ApplicationContext applicationContext) {
        Assert.isNull(ApplicationUtil.applicationContext, "can not set applicationContext once more");
        ApplicationUtil.applicationContext = applicationContext;
    }

    /** 获取指定类型的对象 */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    private static ApplicationContext getApplicationContext() {
        Assert.notNull(applicationContext, "applicationContext not init yet");
        return applicationContext;
    }
}